home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / qbbs / cfix101.zip / CITYFIX.PAS < prev   
Pascal/Delphi Source File  |  1990-02-19  |  13KB  |  402 lines

  1. {$M 16384,0,0}
  2.  
  3. {**************************************************************************}
  4. {*                                      *}
  5. {* CityFix; Capitalizes the City field in QuickBBS's USERS.BBS so         *}
  6. {* it won't look so ugly! Written for QuickBBS 2.04 - 2.63                *}
  7. {* Questions/Comments contact Chris Lamprecht at 1:382/36 FidoNet         *}
  8. {* Compiled with Borland's Turbo Pascal Compiler version 5.5              *}
  9. {*                                          *}
  10. {**************************************************************************}
  11.  
  12. {/* NOTE: Since I get the LAST word from City/State field, two word
  13. Cities (South Dakota, etc) cannot be checked correctly!  That is why
  14. the source is here.  I have assumed anything ending with "York" is New
  15. York, but...  Have fun */}
  16.  
  17. Program CityFix;
  18.  
  19. Uses Dos, Crt;
  20.  
  21. Const
  22.  Version = 'v1.01';
  23.  UserFile = 'USERS.BBS';
  24.  
  25. Type
  26.   CityStr = String[25];
  27.  
  28. {$I STRUCT.263}
  29.  
  30. Var
  31.   X, Loop, G : Integer;
  32.   F, T       : File of UserRecord;
  33.   Exp, Compr,
  34.   StripPeriod,
  35.   KillBack   : Boolean;
  36.   State      : Array[1..65] of String[3];
  37.  
  38.  
  39. Function FileExist(FileName:String):Boolean;
  40. Var f : file;
  41. Begin
  42.   {$I-}
  43.   Assign(f,filename);
  44.   Reset(f);
  45.   Close(f);
  46.   {$I+}
  47.   FileExist := (IOResult = 0) and (FileName <> '');
  48. End;
  49.  
  50. Function Strip(TCity:CityStr):CityStr;   { Strips periods }
  51. Begin
  52.   Repeat
  53.     If Pos('.',TCity) <> 0 then Delete(TCity,Pos('.',TCity),1);
  54.   Until Pos('.',TCity) = 0;
  55.   Strip := TCity;
  56. End;
  57.  
  58. Function Lower(Str:string):string;   { Makes Str all lowercase }
  59. var
  60.   I : integer;
  61. begin
  62.   For I := 1 to length(Str) do
  63.     If ord(Str[I]) in [65..90] then
  64.       Str[I] := chr(ord(Str[I]) + 32);
  65.   Lower := Str;
  66. End; {Func Lower}
  67.  
  68. Function Proper(Str:string):string;   { caps the first letter of words }
  69. var
  70.  I : integer;
  71.  SpaceBefore: boolean;
  72. begin
  73.   SpaceBefore := true;
  74.   Str := lower(Str);
  75.   For I := 1 to length(Str) do
  76.     {If SpaceBefore and (ord(Str[I]) in [97..122]) then}
  77.     If SpaceBefore and (ord(Str[I]) in [97..173]) then
  78.     begin
  79.       SpaceBefore := False;
  80.       Str[I] := Upcase(Str[I]);
  81.     end
  82.     else
  83.       If (SpaceBefore = False) and (Str[I] = ' ') then
  84.         SpaceBefore := true;
  85.   Proper := Str;
  86. end;
  87.  
  88. Function Cap(TCity:CityStr):CityStr;       { fixes some spacing }
  89. Begin
  90.  For x := 1 to length(TCity)-1 do
  91.  Begin
  92.   If TCity[x] = ' ' then
  93.    If TCity[x+1] = ',' then
  94.     Delete(TCity,x,1);
  95.  End;
  96.  For X := 1 to Length(TCity)-1 do
  97.  Begin
  98.   If TCity[x] = ',' then
  99.    if TCity[x+1] <> ' ' then Insert(' ',TCity,x+1);
  100.  End;
  101. { Repeat
  102.   If (Pos(',',TCity) = 0) and (Pos(' ',TCity) > 0) then
  103.    Insert(',',TCity, Pos(' ',TCity));
  104.  Until Pos(',',TCity) <> 0; }
  105.  Cap := Proper(TCity);
  106. End;
  107.  
  108. Function Last(N:byte;Str:string):string;    { Gets last word in City,St }
  109. var Temp : string;
  110. begin
  111.   If N > length(Str) then
  112.     Temp := Str
  113.   else
  114.     Temp := copy(Str,succ(length(Str) - N),N);
  115.   Last := Temp;
  116. end; {Func Last}
  117.  
  118. Function LastPos(C:Char;Str:string):byte;    { Finds last position of char }
  119. Var I : byte;
  120. begin
  121.   I := succ(Length(Str));
  122.   Repeat
  123.      I := Pred(I);
  124.   Until (I = 0) or (Str[I] = C);
  125.   LastPos := I;
  126. end; {Func LastPos}
  127.  
  128. Function Expand(TCity:CityStr):CityStr;     { expands abbr. to state }
  129. Var
  130.   City,
  131.   State : String[25];
  132.  
  133. Begin
  134.   City := Copy(TCity,1,LastPos(' ',TCity));
  135.   State := Copy(TCity,Succ(LastPos(' ',TCity)),length(TCity));
  136.   If State = 'Al' then State := 'Alabama';
  137.   If State = 'Ak' then State := 'Alaska';
  138.   If State = 'Az' then State := 'Arizona';
  139.   If State = 'Ar' then State := 'Arkansas';
  140.   If State = 'Ca' then State := 'California';
  141.   If State = 'Co' then State := 'Colorado';
  142.   If State = 'Ct' then State := 'Connecticut';
  143.   If State = 'De' then State := 'Delaware';
  144.   If State = 'Dc' then State := 'District of Columbia';
  145.   If State = 'Fl' then State := 'Florida';
  146.   If State = 'Ga' then State := 'Georgia';
  147.   If State = 'Gu' then State := 'Guam';             { Guam!?? }
  148.   If State = 'Hi' then State := 'Hawaii';
  149.   If State = 'Id' then State := 'Idaho';
  150.   If State = 'Il' then State := 'Illinois';
  151.   If State = 'In' then State := 'Indiana';
  152.   If State = 'Ia' then State := 'Iowa';
  153.   If State = 'Ks' then State := 'Kansas';
  154.   If State = 'Ky' then State := 'Kentucky';
  155.   If State = 'La' then State := 'Louisiana';
  156.   If State = 'Me' then State := 'Maine';
  157.   If State = 'Md' then State := 'Maryland';
  158.   If State = 'Ma' then State := 'Massachusetts';
  159.   If State = 'Mi' then State := 'Michigan';
  160.   If State = 'Mn' then State := 'Minnesota';
  161.   If State = 'Ms' then State := 'Mississippi';
  162.   If State = 'Mo' then State := 'Missouri';
  163.   If State = 'Mt' then State := 'Montana';
  164.   If State = 'Ne' then State := 'Nebraska';
  165.   If State = 'Nv' then State := 'Nevada';
  166.   If State = 'Nh' then State := 'New Hampshire';
  167.   If State = 'Nj' then State := 'New Jersey';
  168.   If State = 'Nm' then State := 'New Mexico';
  169.   If State = 'Ny' then State := 'New York';
  170.   If State = 'Nc' then State := 'North Carolina';
  171.   If State = 'Nd' then State := 'North Dakota';
  172.   If State = 'Oh' then State := 'Ohio';
  173.   If State = 'Ok' then State := 'Oklahoma';
  174.   If State = 'Or' then State := 'Oregon';
  175.   If State = 'Pa' then State := 'Pennsylvania';
  176.   If State = 'Pr' then State := 'Puerto Rico';
  177.   If State = 'Ri' then State := 'Rhode Island';
  178.   If State = 'Sc' then State := 'South Carolina';
  179.   If State = 'Sd' then State := 'South Dakota';
  180.   If State = 'Tn' then State := 'Tennessee';
  181.   If State = 'Tx' then State := 'Texas';
  182.   If State = 'Ut' then State := 'Utah';
  183.   If State = 'Vt' then State := 'Vermont';
  184.   If State = 'Va' then State := 'Virginia';
  185.   If State = 'Wa' then State := 'Washington';
  186.   If State = 'Wv' then State := 'West Virginia';
  187.   If State = 'Wi' then State := 'Wisconsin';
  188.   If State = 'Wy' then State := 'Wyoming';
  189.   If State = 'Bc' then State := 'British Columbia';
  190.   If State = 'Mb' then State := 'Manitoba';
  191.   If State = 'Nb' then State := 'New Brunswick';
  192.   If State = 'Nf' then State := 'Newfoundland';
  193.   If State = 'Ns' then State := 'Nova Scotia';
  194.   If State = 'On' then State := 'Ontario';
  195.   If State = 'Pe' then State := 'Prince Edward Island';
  196.   If State = 'Pq' then State := 'Quebec';
  197.   If State = 'Sk' then State := 'Saskatchewan';
  198.   If State = 'Yk' then State := 'Yukon';
  199.   If State = 'Nw' then State := 'Northwest Territories';
  200.   Expand := City + State;
  201. End;
  202.  
  203. Function Compress(TCity:CityStr):CityStr;    { compress state to abbrev. }
  204. Var
  205.   City,
  206.   State : String[25];
  207.  
  208. Begin
  209.   City := Copy(TCity,1,LastPos(' ',TCity));
  210.   State := Copy(TCity,Succ(LastPos(' ',TCity)),Length(TCity));
  211.   If State = 'Alabama' then State := 'AL';
  212.   If State = 'Alaska' then State := 'AK';
  213.   If State = 'Arizona' then State := 'AZ';
  214.   If State = 'Arkansas' then State := 'AR';
  215.   If State = 'California' then State := 'CA';
  216.   If State = 'Colorado' then State := 'CO';
  217.   If State = 'Connecticut' then State := 'CT';
  218.   If State = 'Delaware' then State := 'DE';
  219.   If State = 'District of Columbia' then State := 'DC';   {*}
  220.   If State = 'Florida' then State := 'FL';
  221.   If State = 'Georgia' then State := 'GA';
  222.   If State = 'Guam' then State := 'GU';
  223.   If State = 'Hawaii' then State := 'HI';
  224.   If State = 'Idaho' then State := 'ID';
  225.   If State = 'Illinois' then State := 'IL';
  226.   If State = 'Indiana' then State := 'IA';
  227.   If State = 'Iowa' then State := 'IO';
  228.   If State = 'Kansas' then State := 'KS';
  229.   If State = 'Kentucky' then State := 'KY';
  230.   If State = 'Louisiana' then State := 'LA';
  231.   If State = 'Maine' then State := 'ME';
  232.   If State = 'Maryland' then State := 'MD';
  233.   If State = 'Massachusetts' then State := 'MA';
  234.   If State = 'Michigan' then State := 'MI';
  235.   If State = 'Minnesota' then State := 'MN';
  236.   If State = 'Mississippi' then State := 'MS';
  237.   If State = 'Missouri' then State := 'MO';
  238.   If State = 'Montana' then State := 'MT';
  239.   If State = 'Nebraska' then State := 'NE';
  240.   If State = 'Nevada' then State := 'NV';
  241.   If State = 'Hampshire' then State := 'NH';
  242.   If State = 'Jersey' then State := 'NJ';
  243.   If State = 'Mexico' then State := 'NM';
  244.   If State = 'New York' then State := 'NY';      {*}
  245.   If State = 'Morth Carolina' then State := 'NC';     {*}
  246.   If State = 'North Dakota' then State := 'ND';       {*}
  247.   If State = 'Ohio' then State := 'OH';
  248.   If State = 'Oklahoma' then State := 'OK';
  249.   If State = 'Oregon' then State := 'OR';
  250.   If State = 'Pennsylvania' then State := 'PA';
  251.   If State = 'Puerto Rico' then State := 'PR';        {*}
  252.   If State = 'Rhode Island' then State := 'RI';       {*}
  253.   If State = 'South Carolina' then State := 'SC';     {*}
  254.   If State = 'South Dakota' then State := 'SD';       {*}
  255.   If State = 'Tennessee' then State := 'TN';
  256.   If State = 'Texas' then State := 'TX';
  257.   If State = 'Utah' then State := 'UT';
  258.   If State = 'Vermont' then State := 'VT';
  259.   If State = 'Virginia' then State := 'VA';
  260.   If State = 'Washington' then State := 'WA';
  261.   If State = 'West Virginia' then State := 'WV';       {*}
  262.   If State = 'Wisconsin' then State := 'WI';
  263.   If State = 'Wyoming' then State := 'WY';
  264.   If State = 'Alberta' then State := 'AB';
  265.   If State = 'Columbia' then State := 'BC';
  266.   If State = 'Manitoba' then State := 'MB';
  267.   If State = 'Brunswick' then State := 'NB';
  268.   If State = 'Newfoundland' then State := 'NF';
  269.   If State = 'Scotia' then State := 'NS';
  270.   If State = 'Ontario' then State := 'ON';
  271.   If State = 'Prince Edward Island' then State := 'PE';   {*}
  272.   If (State = 'Quebec') or (State = 'Québec') then State := 'PQ';
  273.   If State = 'Saskatchewan' then State := 'SK';
  274.   If State = 'Yukon' then State := 'YK';
  275.   If State = 'Territories' then State := 'NW';
  276.   Compress := City + State;
  277. End;
  278.  
  279. {*} { = Two word state - I was too lazy to think on this one. }
  280.  
  281.  
  282. Procedure Backup;        { Backs up users.bbs with 4K buffer }
  283. Var
  284.  FromF, ToF : File;
  285.  NRead,
  286.  NWritten  : Word;
  287.  Buf    : Array[1..4096] of Char;
  288.  
  289. Begin
  290.   TextColor(2);
  291.   Writeln('Writing backup file ''USERS.BAK''',#13,#10);
  292.   Assign(FromF, UserFile);
  293.   Reset(FromF, 1);
  294.   Assign(ToF, 'USERS.BAK');
  295.   Rewrite(ToF, 1);
  296.   Repeat
  297.     BlockRead(FromF, buf, SizeOf(buf), NRead);
  298.     BlockWrite(ToF, buf, NRead, NWritten);
  299.   Until (NRead = 0) or (NWritten <> NRead);
  300.   Close(FromF);
  301.   Close(ToF);
  302. End;
  303.  
  304. Procedure HelpScreen;   { of all things; the helpscreen }
  305. Begin
  306.   TextColor(2);
  307.   Writeln('USAGE:'#13,#10);
  308.   TextColor(10);
  309.   Writeln(' CityFix [-B] [-E or -C] [-P]',#13,#10);
  310.   TextColor(2);
  311.   Writeln('-B = Do not erase backup file USERS.BAK');
  312.   Writeln('-C = Compress States/Provinces into Abbreviations');
  313.   Writeln('-E = Expand State/Province Abbreviations');
  314.   Writeln('-P = Strip ALL periods from City/States',#13,#10);
  315.   Writeln('This must be run in the same directory as the USERS.BBS file.');
  316.   TextColor(7);
  317.   Writeln('Exit Code = 1');
  318.   Halt(1);
  319. End;
  320.  
  321. Procedure Init;    { what else! }
  322. Begin
  323.   ClrScr;
  324.   TextColor(3);
  325.   Writeln('CityFix '+Version+' by Chris Lamprecht of FidoNet 1:382/36',#13,#10);
  326.   TextColor(7);
  327.   G := 0;
  328.   If not FileExist('USERS.BBS') then
  329.   Begin
  330.     TextColor(12);
  331.     Writeln('Users.BBS not found in current directory!');
  332.     HelpScreen;
  333.   End;
  334.   Exp := FALSE;
  335.   Compr := FALSE;
  336.   KillBack := TRUE;
  337.   StripPeriod := FALSE;
  338.   For X := 1 to ParamCount do
  339.   Begin
  340.     If (ParamStr(X) = '/?') or (ParamStr(X) = '?') then HelpScreen;
  341.     If (ParamStr(X) = '-E') or (ParamStr(X) = '-e') then Exp := TRUE;
  342.     If (ParamStr(X) = '-C') or (ParamStr(X) = '-c') then Compr := TRUE;
  343.     If (ParamStr(X) = '-B') or (ParamStr(X) = '-b') then KillBack := FALSE;
  344.     If (ParamStr(X) = '-P') or (ParamStr(X) = '-p') then StripPeriod := TRUE;
  345.     If (Compr = TRUE) and (Exp = TRUE) then
  346.     Begin
  347.       Compr := FALSE;
  348.       Exp := FALSE;
  349.       Writeln('You can''t compress States AND Expand states!');
  350.     End;
  351.   End;
  352. End;
  353.  
  354. Procedure Do_It;                 { guts of cityfix }
  355. Var Tuse : UserRecord;
  356.   OldCity : String[25];
  357.  
  358. Begin
  359.   Assign(f,UserFile);
  360.   Reset(f);
  361.   If StripPeriod then
  362.   Begin
  363.     Write('Stripping Periods');
  364.     If Exp or Compr then Write(', ') else Write(' and ');
  365.   End;
  366.   If Exp then Write('Expanding and ');
  367.   If Compr then Write('Compressing and ');
  368.   Writeln('Capitalizing ',FileSize(f),' Users'' Cities/States ...'#13,#10);
  369.   For Loop := 0 to FileSize(f)-1 do
  370.   Begin
  371.     Seek(f,loop);
  372.     Read(f,Tuse);
  373.     OldCity := Tuse.City;
  374.     Tuse.City := Cap(Tuse.City);
  375.     If StripPeriod then Tuse.City := Strip(Tuse.City);
  376.     If Compr then Tuse.City := Compress(Tuse.City);
  377.     If Exp then Tuse.City := Expand(Tuse.City);
  378.     Write(Loop+1:4,' ',OldCity:25,' -> ',Tuse.City,'          ',#13);
  379.     If OldCity <> Tuse.City then G := G + 1;
  380.     Seek(F,loop);
  381.     Write(f,Tuse);
  382.   End;
  383.   Close(f);
  384.   Writeln(#13,#10);
  385.  
  386.   If KillBack then
  387.   Begin
  388.     Writeln('Deleting backup file ''USERS.BAK'''#13,#10);
  389.     Assign(t,'USERS.BAK');
  390.     Erase(t);
  391.   End;
  392.   TextColor(7);
  393.   Write(G,' UserRecords were changed.');
  394.   Halt(0);
  395. End;
  396.  
  397. Begin
  398.   CheckSnow := TRUE;
  399.   Init;
  400.   Backup;
  401.   Do_it;
  402. End.